package com.herui.ota.service.iml;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.herui.ota.dao.mysql.DeviceNewDailyMapper;
import com.herui.ota.entity.DeviceNewDaily;
import com.herui.ota.pojo.vo.DeviceAreaCountVo;
import com.herui.ota.pojo.form.DeviceNewDailyFilterForm;
import com.herui.ota.pojo.vo.DeviceNewDailyStaticVo;
import com.herui.ota.service.DeviceNewDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2023-10-26 16:51:33
 */
@Service
public class DeviceNewDailyServiceIml extends BaseService<DeviceNewDailyMapper, DeviceNewDaily> implements DeviceNewDailyService {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    @Override
    public void setBaseMapper(DeviceNewDailyMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Transactional
    @Override
    public void addCount(String type, String area, int count) {

        if (count == 0){
            return;
        }

        String format = dateFormat.format(new Date());
        DeviceNewDaily deviceNewDaily = getOne(new QueryWrapper<DeviceNewDaily>().eq("date",
                format).eq("type", type).eq("area", area));
        if (deviceNewDaily == null) {
            deviceNewDaily = new DeviceNewDaily();
            deviceNewDaily.setType(type);
            deviceNewDaily.setArea(area);
            deviceNewDaily.setDate(new Date());
            deviceNewDaily.setSize(count);
            save(deviceNewDaily);
        } else {
            deviceNewDaily.setSize(deviceNewDaily.getSize() + count);
            updateById(deviceNewDaily);
        }
    }

    @Override
    public int getCurrentDayTotalCount() {

        int count = count(new QueryWrapper<DeviceNewDaily>().eq("date", DateUtil.beginOfDay(new Date()).toJdkDate()));
        return count;
    }

    @Override
    public List<DeviceNewDailyStaticVo> getWeekDeviceNewDailyStaticVos(DeviceNewDailyFilterForm deviceNewDailyFilterForm) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date endDate = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date startDate = calendar.getTime();

        return getDeviceNewDailyStaticVos(startDate, endDate, deviceNewDailyFilterForm);
    }

    @Override
    public List<DeviceNewDailyStaticVo> getMonthDeviceNewDailyStaticVos(DeviceNewDailyFilterForm deviceNewDailyFilterForm) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date endDate = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        Date startDate = calendar.getTime();

        return getDeviceNewDailyStaticVos(startDate, endDate, deviceNewDailyFilterForm);
    }

    @Override
    public List<DeviceNewDailyStaticVo> getDeviceNewDailyStaticVos(Date start, Date end,
                                                                   DeviceNewDailyFilterForm deviceNewDailyFilterForm) {
        try {
            QueryWrapper<DeviceNewDaily> queryWrapper = getQueryWrapper(deviceNewDailyFilterForm);
            queryWrapper.le("date", end).ge("date", start);

            List<DeviceNewDailyStaticVo> deviceNewDailyStaticVos = baseMapper.selectDeviceNewDailyStaticVo(start, end
                    , deviceNewDailyFilterForm);

            List<DeviceNewDailyStaticVo> tempDeviceNewDailyStaticVos = new LinkedList<>();

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(start);
            calendar.set(Calendar.MILLISECOND, 0);
            for (; calendar.getTime().getTime() <= end.getTime();){
                boolean exist = false;
                for (DeviceNewDailyStaticVo deviceNewDailyStaticVo: deviceNewDailyStaticVos){
//                    log.info("{}, {}", deviceNewDailyStaticVo.getDate(), calendar.getTime());
                    if (deviceNewDailyStaticVo.getDate().equals(calendar.getTime())){
                        tempDeviceNewDailyStaticVos.add(deviceNewDailyStaticVo);
                        exist = true;
                        break;
                    }
                }
                if (!exist){
                    DeviceNewDailyStaticVo deviceNewDailyStaticVo = new DeviceNewDailyStaticVo();
                    deviceNewDailyStaticVo.setDate(calendar.getTime());
                    deviceNewDailyStaticVo.setCount(0);
                    tempDeviceNewDailyStaticVos.add(deviceNewDailyStaticVo);
                }

                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }

            return tempDeviceNewDailyStaticVos;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<DeviceAreaCountVo> getDeviceAreaCountVos() {
        return baseMapper.selectDeviceAreaCountVos();
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date endDate = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        Date startDate = calendar.getTime();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(startDate);
        for (; calendar.getTime().getTime() <= endDate.getTime();){
            System.out.println(dateFormat.format(calendar.getTime()));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}

